import Cookies from 'js-cookie'
import store from './../store'
import { Message } from 'element-ui'
import NProgress from 'nprogress' // Progress 进度条

const TokenKey = 'Admin-Token'

export function getToken() {
  return Cookies.get(TokenKey)
}

export function setToken(token) {
  return Cookies.set(TokenKey, token)
}

export function removeToken() {
  return Cookies.remove(TokenKey)
}

export const powerTree = [{
  code: 'center',
  title: '平台',
  child: [{
    code: 'app',
    title: '应用列表',
    api: '/app/find',
    child: [
      { code: 'appCreate', title: '创建应用', api: '/app/create' },
      { code: 'appShow', title: '查看应用', api: '/app/findOne' },
      { code: 'appEnable', title: '暂停/恢复应用', api: '/app/enable' },
      { code: 'appEdit', title: '编辑应用', api: '/app/update' },
      { code: 'appPush', title: '应用推送配置', api: '/app/update' },
      { code: 'appApi', title: '应用接口配置', api: '/app/update' },
      { code: 'appRemove', title: '删除应用', api: '/app/remove' }
    ]
  }, {
    code: 'staff',
    title: '职员',
    api: '/staff/find'
  }]
}, {
  code: 'power',
  title: '权限',
  child: [{
    code: 'admin',
    title: '管理员',
    api: '/admin/find',
    child: [
      { code: 'adminShow', title: '查看管理员', api: '/admin/findOne' },
      { code: 'adminCreate', title: '新增管理员', api: '/admin/create' },
      { code: 'adminEdit', title: '编辑管理员', api: '/admin/update' },
      { code: 'adminPower', title: '管理员角色分配', api: '/admin/update' },
      { code: 'adminRemove', title: '删除管理员', api: '/admin/remove' }
    ]
  }, {
    code: 'role',
    title: '角色',
    api: '/role/find',
    child: [
      { code: 'roleCreate', title: '创建角色', api: '/role/create' },
      { code: 'roleShow', title: '查看角色', api: '/role/findOne' },
      { code: 'roleEdit', title: '编辑角色', api: '/role/update' },
      { code: 'roleRemove', title: '删除角色', api: '/role/remove' }
    ]
  }]
}, {
  code: 'log',
  title: '日志',
  child: [
    { code: 'logLogin', title: '登录日志', api: '/log/find' },
    { code: 'logAdmin', title: '后台日志', api: '/log/find' },
    { code: 'logPush', title: '接受日志', api: '/log/find' },
    { code: 'logApi', title: '接口日志', api: '/log/find' }
  ]
}]

const childAll = (arr, type) => {
  type = type || 'codeTitle'
  const apiCode = {}
  const o = {}
  arr.forEach((v) => {
    if (v.api) {
      o[v.code] = v.title
      if (!apiCode[v.api]) apiCode[v.api] = v.code
      else {
        if (typeof apiCode[v.api] === 'string') apiCode[v.api] = [apiCode[v.api]]
        apiCode[v.api].push(v.code)
      }
    }
    if (v.child) {
      if (type === 'apiCode') Object.assign(apiCode, childAll(v.child, type))
      else Object.assign(o, childAll(v.child, type))
    }
  })
  if (type === 'apiCode') return apiCode
  return o
}

// 日志代码
export const logCodes = childAll(powerTree)
console.log(childAll(powerTree, 'apiCode'))

export const logType = {
  adminRemove: '删除管理员',
  accountLogin: '登录帐号',
  accountLogout: '注销帐号',
  accountPasswd: '修改密码',
  staffRemove: '删除职员'
}

// 推送钩子选项
export const logTypePush = {
  staffCreate: '职员新增',
  staffUpdate: '职员信息更新',
  staffPasswd: '职员密码更新'
}
// 服务接口选项
export const logTypeApi = Object.assign({
  staffLogin: '登录接口',
  staffFind: '职员列表',
  staffFindOne: '职员详情'
}, logTypePush)

const uriPowers = { // 路由权限表 uri > powers
  '/': '',
  '/dashboard': '',
  '/account/login': '',
  '/password': '',
  '/app/add': 'appCreate', // 'appCreate',
  '/app/enable': ['appEnable'], // 'appEnable',
  '/app/edit': ['appEdit'],
  '/app/list': ['app', 'appCreate', 'appEdit', 'appEnable', 'appPush', 'appApi', 'appShow', 'appRemove'],
  '/app/show': 'appShow',
  '/app/remove': 'appRemove',
  '/staff/list': ['staff'],
  '/log/push': '', // 'logPush',
  '/log/api': '', // 'logApi',
  '/log/admin': '', // 'logAdmin',
  '/log/login': '', // 'logLogin',
  '/log/test': '', // 'logLogin',
  '/admin/list': ['admin', 'adminShow', 'adminCreate', 'adminRemove', 'adminEdit', 'adminFindOne'],
  '/admin/add': 'adminCreate',
  '/admin/edit': ['adminEdit', 'adminFindOne'],
  '/admin/remove': 'adminRemove',
  '/role/list': ['role', 'roleRemove', 'roleCreate', 'roleShow', 'roleEdit'],
  '/role/add': 'roleCreate',
  '/role/edit': ['roleEdit', 'roleFindOne'],
  '/role/remove': 'roleRemove'
}

export function hasPower(to, next) {
  var powers = store.getters.role.powers
  // url 判断任意保护代码
  var uri = to
  if (to.path) uri = to.path
  // 代码直接判断是否存在
  var powerStr = ',' + powers.join(',')

  function call() {
    if (rs) {
      if (next) next()
      return rs
    }
    if (next) Message.error('无权访问')
    NProgress.done()
    if (next) next(false)
    return rs
  }

  var rs = false
  if (store.getters.username === 'admin') {
    rs = true
    return call()
  }
  if (uri.indexOf('/') === -1) {
    rs = powerStr.indexOf(',' + uri) !== -1
    return call()
  }
  var up = uriPowers[uri]
  if (typeof up === 'undefined') return call()
  if (!powers || powers.length === 0) return call()
  if (up === '') {
    rs = true
    return call()
  }
  if (typeof up === 'string') up = [up]
  up.forEach(v => {
    if (powers.indexOf(v) !== -1) rs = true
  })
  return call()
}

import Vue from 'vue'
Vue.mixin({
  methods: { hasPower: hasPower }
})
